using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Loading.Models;
using Loading.ViewModel;

namespace Loading.Controllers
{   
	public class CartrigeLotsController : Controller
	{
		private readonly ICartrigeLotRepository _cartrigelotRepository;

		// If you are using Dependency Injection, you can delete the following constructor
		public CartrigeLotsController() : this(new CartrigeLotRepository())
		{
		}

		public CartrigeLotsController(ICartrigeLotRepository cartrigelotRepository)
		{
			_cartrigelotRepository = cartrigelotRepository;
		}

		//
		// GET: /CartrigeLots/

		public ViewResult Index()
		{
			return View(_cartrigelotRepository.AllIncluding(cartrigelot => cartrigelot.Notes));
		}

		//
		// GET: /CartrigeLots/Details/5

		public ViewResult Details(int id)
		{
			return View(_cartrigelotRepository.Find(id));
		}

		//
		// GET: /CartrigeLots/Create

		public ActionResult Create()
		{
            CartrigeDataViewModel dataViewModel = new CartrigeDataViewModel(_cartrigelotRepository);
			return View();
		} 

		//
		// POST: /CartrigeLots/Create

		[HttpPost]
		public ActionResult Create(CartrigeLot cartrigelot)
		{
		    if (ModelState.IsValid) {
				_cartrigelotRepository.InsertOrUpdate(cartrigelot);
				_cartrigelotRepository.Save();
				return RedirectToAction("Index");
			}
		    return View();
		}

	    //
		// GET: /CartrigeLots/Edit/5
 
		public ActionResult Edit(int id)
		{
			_cartrigelotRepository.Find(c => c.Brass.TimesFired < 5 & c.IsDiscarded==false);
			 return View(_cartrigelotRepository.Find(id));

		}

		//
		// POST: /CartrigeLots/Edit/5

		[HttpPost]
		public ActionResult Edit(CartrigeLot cartrigelot)
		{
			if (ModelState.IsValid) {
				_cartrigelotRepository.InsertOrUpdate(cartrigelot);
				_cartrigelotRepository.Save();
				return RedirectToAction("Index");
			} else {
				return View();
			}
		}

		//
		// GET: /CartrigeLots/Delete/5
 
		public ActionResult Delete(int id)
		{
			return View(_cartrigelotRepository.Find(id));
		}

		//
		// POST: /CartrigeLots/Delete/5

		[HttpPost, ActionName("Delete")]
		public ActionResult DeleteConfirmed(int id)
		{
			_cartrigelotRepository.Delete(id);
			_cartrigelotRepository.Save();

			return RedirectToAction("Index");
		}
	}
}

